Uec3.DepartmentQueryWindow = Ext.extend(Ext.app.Module, {
	id : 'departmentQuery',
	type : 'common/departmentQuery',

	/**
	 * Initialize this module. This function is called at startup (page
	 * load/refresh).
	 */
	init : function(initParams) {

	},
	paramsCollection : new Ext.util.MixedCollection(true),

	getParamsByKey : function(key) {
		return this.paramsCollection.get(key);
	},
	selectDepartmentAddress : function(rowIndex) {

		var department_grid = this.getParamsByKey('department_grid');

		var selectModel = department_grid.getSelectionModel();

		selectModel.selectRow(rowIndex);

		var record = selectModel.getSelected();

		this.setLookupValue(record.data, department_grid.ownerCt);

		department_grid.ownerCt.close();

	},

	getComponents : function(scope) {

		var department_ds;
		var department_grid;
		var department_form;
		var department_ajax;

		function renderDesc(value, cellmeta, record, rowIndex, columnIndex,
				store) {

			var str = "<a style='font-size:12px' href='javascript:Ext.CUR_APP.getModule(\"departmentQuery\").selectDepartmentAddress("
					+ rowIndex + ");'>选中</a>";

			return str;

		};

		var getAndRestoreDepartment = function() {

			department_ds = new Ext.data.Store({
						// proxy: new Ext.data.MemoryProxy(myData),
						proxy : new Ext.data.HttpProxy({
									url : Uec3.App.Constants.homeUrl
											+ '/department/base!query.do'
								}),
						baseParams : {
							start : 0,
							limit : 10
						},
						reader : new Ext.data.JsonReader({
									totalProperty : 'totalProperty',
									root : 'root'
								}, [{
											name : 'desc'
										}, {
											name : 'id'
										}, {
											name : 'companyId'
										}, {
											name : 'pyCode'
										}, {
											name : 'name'
										}, {
											name : 'gmtCreate',
											type : 'string'
										}, {
											name : 'gmtModify',
											type : 'string'
										}

								]),
						remoteSort : true,

						// 默认排序字段
						sortInfo : {
							field : "id",
							direction : "asc"
						}

					});
		};

		var componentInitsDepartment = function() {

			var department_colModel = new Ext.grid.ColumnModel([
					new Ext.grid.RowNumberer(), {
						header : "操作",
						width : 80,
						dataIndex : 'desc',
						align : 'center',
						renderer : renderDesc
					}, {
						header : "ID",
						width : 60,
						dataIndex : 'id',
						hidden : true
					}, {
						header : "companyId",
						width : 60,
						dataIndex : 'companyId',
						hidden : true
					}, {
						header : "部门名称",
						width : 80,
						sortable : true,
						dataIndex : 'name'
					},

					{
						header : "创建时间",
						width : 150,
						dataIndex : 'gmtCreate'
					}, {
						header : "修改时间",
						width : 150,
						dataIndex : 'gmtModify'
					}

			]);
			// create the Grid
			department_grid = new Ext.grid.GridPanel(

			{
						// title:'部门信息列表',
						region : 'center',
						iconCls : 'icon-grid',
						enableColumnMove : false,
						enableColumnMoveResize : false,
						stripeRows : true,
						height : 600,
						loadMask : {
							msg : "...加载数据中"
						},
						store : department_ds,
						columnLines : false,
						cm : department_colModel,
						sm : new Ext.grid.RowSelectionModel({
									singleSelect : true
								}),
						viewConfig : {
							forceFit : true,
							columnsText : '显示的列',
							scrollOffset : 20,
							sortAscText : '升序',
							sortDescText : '降序'
						},
						bbar : new Ext.PagingToolbar({
									pageSize : 10,
									store : department_ds,
									display : true,
									displayInfo : true,
									displayMsg : '第 {0} - {1} 条  共 {2} 条',
									emptyMsg : "没有记录",
									items : [/**
												 * '-', { pressed: true,
												 * enableToggle:true, text:
												 * 'Show Preview', cls:
												 * 'x-btn-text-icon details',
												 * toggleHandler: function(btn,
												 * pressed){ var view =
												 * department_grid.getView();
												 * view.showPreview = pressed;
												 * view.refresh(); } }
												 */
									]
								})

					});

			//department_ds.load();

		};

		var controlDepartmentEvent = function() {

			department_grid.on('rowdblclick', function() {

						var me = this;

						var rowId = arguments[1];

						var record = me.getStore().getAt(rowId);

						scope.setLookupValue(record.data, me.ownerCt);

						me.ownerCt.close();

					})

			/**
			 * department_ds.on('load', function() { var me = this; var records =
			 * me.getRange(); Ext.each(records, function(record) {
			 * 
			 * var rowId = me.indexOf(record);
			 * Ext.get('selectDepartmentAddress_' + rowId) .on('click',
			 * function() {
			 * 
			 * selectDepartmentAddress(rowId);
			 * 
			 * });
			 * 
			 * });
			 * 
			 * });
			 */

		};

		getAndRestoreDepartment();

		componentInitsDepartment();

		controlDepartmentEvent();
		this.paramsCollection.add("department_ds", department_ds);
		this.paramsCollection.add("department_grid", department_grid);

		return department_grid;

	},
	initEvent:function(){
		var module=this;
		var win=module.getParamsByKey("win");
		win.on('show',function(){
			this.getParamsByKey("department_ds").load({
				 callback: function(records,options,success){
		         
		         if(success){
		        	 
		        	// Ext.Msg.alert('提示',"加载数据成功");
		         }else{
		        	 
		        	 
		        	 Ext.Msg.alert('提示',"服务器正在升级中...请稍候再试");
		         }			  
			
		 }			
	
	});
		},module,{single:true});
	},
	/**
	 * Create this modules window here.
	 */
	createWindow : function() {
		var desktop = this.app.getDesktop();
		var module=this;
		var win = desktop.getWindow('departmentQuery');
		if (!win) {
			win = desktop.createWindow({
						id : 'departmentQuery',
						title : '部门列表',
						width : 500,
						modal : true,
						height : 330,
						iconCls : 'acc-icon',
						shim : false,
						animCollapse : false,
						constrainHeader : true,
						maximizable : false,
						taskbuttonTooltip : "部门列表",
						resizable : false,
						layout : "fit",
						items : [this.getComponents(this)]
					});
			 module.paramsCollection.add("win",win);
	         module.initEvent();
		}
		win.show();

	}
});